import 'dart:convert';
import 'dart:ffi';
import 'dart:io';
import 'package:crypto/crypto.dart';
import 'package:dio/dio.dart';
import 'package:flutter6/models/Song.dart';

import '../models/MusicList.dart';

String byteArrayToHexString(List<int> byteArray) {
  StringBuffer stringBuffer = StringBuffer();
  for (int i = 0; i < byteArray.length; i++) {
    stringBuffer.write(byteArray[i].toRadixString(16).padLeft(2, '0'));
  }
  return stringBuffer.toString();
}

Future<String> get(String paramString2) async {
  String paramString1 = "qq";
  String type = "mp3";
  // String paramString2 = "003gSjsG4eGvOJ";
  String str2 = "Redmi";
  String str3 = "11";
  int l = DateTime.now().millisecondsSinceEpoch ~/ 1000;
  // l = 1701746442;
  String stringBuilder1 = "d86b856be4a7ea7a5bc9b6c4eed46f4e$l"
      "6562653262383463363633646364306534333668";
  String str1 =
      md5.convert(utf8.encode(stringBuilder1)).toString().toLowerCase();
  StringBuffer stringBuilder2 = StringBuffer();
  stringBuilder2
      .write("{\\\"method\\\":\\\"GetMusicUrl\\\",\\\"platform\\\":\\\"");
  stringBuilder2.write(paramString1);
  stringBuilder2.write("\\\",\\\"t1\\\":\\\"");
  stringBuilder2.write(paramString2);
  stringBuilder2.write("\\\",\\\"t2\\\":\\\"");
  stringBuilder2.write(type);
  stringBuilder2.write("\\\"}");
  paramString2 = stringBuilder2.toString();
  StringBuffer stringBuilder4 = StringBuffer();
  stringBuilder4.write(
      "{\\\"uid\\\":\\\"\\\",\\\"token\\\":\\\"\\\",\\\"deviceid\\\":\\\"84ac82836212e869dbeea73f09ebe52b\\\",\\\"appVersion\\\":\\\"");
  stringBuilder4.write("4.1.4");
  stringBuilder4.write("\\\",\\\"vercode\\\":\\\"");
  stringBuilder4.write("4140");
  stringBuilder4.write("\\\",\\\"device\\\":\\\"");
  stringBuilder4.write(str2);
  stringBuilder4.write("\\\",\\\"osVersion\\\":\\\"");
  stringBuilder4.write(str3);
  stringBuilder4.write("\\\"}");
  String str5 = stringBuilder4.toString();
  StringBuffer stringBuilder5 = StringBuffer();
  stringBuilder5.write("{\n\t\"text_1\":\t\"");
  stringBuilder5.write(paramString2);
  stringBuilder5.write("\",\n\t\"text_2\":\t\"");
  stringBuilder5.write(str5);
  stringBuilder5.write("\",\n\t\"sign_1\":\t\"");
  stringBuilder5.write(str1);
  stringBuilder5.write("\",\n\t\"time\":\t\"");
  stringBuilder5.write(l);
  stringBuilder5.write("\",\n\t\"sign_2\":\t\"");
  StringBuffer stringBuilder6 = StringBuffer();
  stringBuilder6.write(paramString2.replaceAll("\\", ""));
  stringBuilder6.write(str5.replaceAll("\\", ""));
  stringBuilder6.write(str1);
  stringBuilder6.write(l);
  stringBuilder6.write("NDRjZGIzNzliNzEe");
  stringBuilder5.write(md5
      .convert(utf8.encode(stringBuilder6.toString()))
      .toString()
      .toLowerCase());
  stringBuilder5.write("\"\n}");
  ZLibCodec codec = ZLibCodec();
  List<int> abc = codec.encode(utf8.encode(
      byteArrayToHexString(utf8.encode(stringBuilder5.toString()))
          .toUpperCase()));
  final dio = Dio();
  final rs = await dio.post(
    'http://app.kzti.top/client/cgi-bin/api.fcg',
    data: abc,
    options:
        Options(responseType: ResponseType.bytes, contentType: 'text/plain'),
  );
  List<int> decompressedData = codec.decode(rs.data);
  String ru = utf8.decode(decompressedData);
  Map<String, dynamic> jsonMap = jsonDecode(ru);
  return jsonMap['data'];
}

Future<MusicList> getMusicList() async {
  final dio = Dio();
  Response response = await dio.get(
    'https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg?type=1&json=1&utf8=1&onlysong=0&disstid=693837784&format=json&format=jsonp&g_tk=5381&loginUin=1&hostUin=0&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0spa',
    options: Options(headers: {
      'Referer': 'https://y.qq.com/n/yqq/playlist',
    }),
  );
  Map<String, dynamic> coll = response.data['cdlist'][0];
  List<dynamic> res = coll['songlist'];
  var list = MusicList(
    nickname: coll['nickname'],
    dissname: coll['dissname'],
    dissimg: coll['logo'],
    headurl: coll['headurl'],
    songlist: <Song>[],
  );
  for (var item in res) {
    String albummid = item['albummid'];
    String singermid = item['singer'][0]['mid'];
    list.songlist?.add(Song(
      songid: item['songid'],
      songmid: item['songmid'],
      songname: item['songname'],
      albumname: item['albumname'],
      interval: item['interval'],
      singername: item['singer'][0]['name'],
      singermid: item['singer'][0]['mid'],
      size128: item['size128'],
      size320: item['size320'],
      sizeflac: item['sizeflac'],
      songimg: item['albummid']== ''?'https://y.qq.com/music/photo_new/T001R300x300M000$singermid.jpg':'https://y.qq.com/music/photo_new/T002R300x300M000$albummid''_1.jpg',
    ));
  }
  return list;
}

Future<void> main() async {
  print(await getMusicList());
}
